#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2023
@author: GLTS
"""
import os
import shutil
import time
import tkinter
import tkinter.messagebox

import requests


def download(url_temp, name, path='here'):
    try:
        get_temp = requests.get(url_temp)
        with open(name, 'wb') as download_temp:
            download_temp.write(get_temp.content)
        find_temp = os.path.exists(name)
        if find_temp:
            if path == 'here':
                return 'DT'
            else:
                try:
                    shutil.move(name, path)
                    return 'DTM'
                except:
                    return 'DTUM'
        elif not find_temp:
            return 'DF'
    except:
        return 'DF'


def readtxt(num, path):
    find_temp = os.path.exists(path)
    if find_temp:
        read_temp = open(path, 'r')
        for i_temp in range(num):
            if i_temp == num - 1:
                return read_temp.readline()
            else:
                read_temp.readline()
        read_temp.close()
    elif not find_temp:
        return '\033[0;31;1m' + "Error:Can't find " + path + '\033[0m'


url = "https://gitee.com/GLTSy/Update-Information-Database/raw/main/Life%20Assistant%20Update/Update_Information.ini"
value_download = download(url, 'Update_Information.ini')


if value_download == 'DT':

    print('下载成功')
    value_find1 = os.path.exists("Update_Information.ini")
    value_find2 = os.path.exists("Configuration.ini")
    if value_find1 and value_find2:
        value_read1 = readtxt(3, "Update_Information.ini")
        value_read2 = readtxt(3, "Configuration.ini")
        if value_read1[15:-1] == value_read2[16:-1]:
            print('没有更新')
            tkinter.messagebox.showinfo('提示', '没有可用更新')
        else:
            print('当前版本为 ' + value_read2[16:-1] + '，最新版本为 ' + value_read1[
                                                                        15:-1] + '\n是否更新?请输入/Yes或/No')
    elif not value_find1 or not value_find2:
        print('出现错误无法找到文件，请重试')
        tkinter.messagebox.showerror('错误', '出现错误无法找到文件，请重试')
else:

    print('\r出现错误无法下载文件，请重试')
    tkinter.messagebox.showerror('错误', '出现错误无法找到文件，请重试')
